package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntGoalCheckMapper;
import com.prd.model.EntGoalCheck;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalCheckService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntGoalCheckServiceImpl extends ServiceImpl<EntGoalCheckMapper, EntGoalCheck> implements EntGoalCheckService {

    @Autowired
    private EntGoalCheckMapper entGoalCheckMapper;

    @Override
    public int insertInfo(EntGoalCheck entGoalCheck) {
        LambdaQueryWrapper<EntGoalCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntGoalCheck::getGoalId, entGoalCheck.getGoalId());
        queryWrapper.eq(EntGoalCheck::getType, entGoalCheck.getType());
        EntGoalCheck goalCheck = entGoalCheckMapper.selectOne(queryWrapper);
        if (goalCheck != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalCheck);
        return entGoalCheckMapper.insert(entGoalCheck);
    }

    @Override
    public int updateInfo(EntGoalCheck entGoalCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalCheck);
        return entGoalCheckMapper.updateById(entGoalCheck);
    }
}
